//
//  HZDrawerViewController.m
//  MyDoctor
//
//  Created by 黑眼圈 on 16/5/4.
//  Copyright © 2016年 heiyan. All rights reserved.
// 抽屉主控制器

#import "HZDrawerViewController.h"

@interface HZDrawerViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,strong)NSArray*  array;

@end

@implementation HZDrawerViewController


//判断是否登录
bool iss = YES;


-(void)loadView{
    
    //如果登录成功
    if (iss) {
        
        UITableView* tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        self.view = tableView;
        
    }else{
       // 未登录
        
        HZHeadLoginView* headView = [[HZHeadLoginView alloc]init];
        
        
        self.view = headView;
    }
    
 
        
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //如果未登录接收完注册和登录按钮的通知之后直接返回
    if (!iss) {
        
        //接收通知
        [self receiveNoti];
        
        return;
    }
    
    //设置并添加子控件
    [self setupUI];
    
 
    
}



#pragma mark -
#pragma mark     设置并添加子控件
-(void)setupUI{
    
    UITableView* tableView = (UITableView*)self.view;
    
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    
    //注册 cell
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"preCell"];
    
    //设置头部视图
    HZPersonageView* perView = [[HZPersonageView alloc]init];
    
    __weak HZDrawerViewController *weakSelf = self;
    //回调的block
    perView.pushInfoVC = ^{
        NSLog(@"block被调用了");
        
        HZMainViewController *mainVC = [[HZMainViewController alloc]init];
        
        
        [weakSelf.mm_drawerController setCenterViewController:[[UINavigationController alloc]initWithRootViewController:mainVC] withCloseAnimation:YES completion:nil];
        
        
        HZBasicInfoController *basicVC = [[HZBasicInfoController alloc]init];
        

        
        [mainVC.navigationController pushViewController:basicVC animated:YES];
        
    };
    
    perView.height = 100;
    
    tableView.tableHeaderView = perView;
}

#pragma mark -
#pragma mark     懒加载数组
-(NSArray *)array{
    if (!_array) {
        
        _array = [HZPreferencesModel getpreferencesArray];
    }
    return _array;
}




#pragma mark -
#pragma mark    tableView 的数据源代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSLog(@"睡觉");
    
    //获取数据
    HZPreferencesModel* model = self.array[indexPath.section][indexPath.row];
    
    HZMainViewController *mainVC = [[HZMainViewController alloc]init];
    
    
    [self.mm_drawerController setCenterViewController:[[UINavigationController alloc]initWithRootViewController:mainVC] withCloseAnimation:YES completion:nil];
    
    
    if (indexPath.section == 0) {
        
        switch (indexPath.row) {
            case 0:{
                
                //创建登录控制器
                HZFamousDoctorViewController* famousDoctorVc = [[HZFamousDoctorViewController alloc]init];
                
                famousDoctorVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:famousDoctorVc animated:NO];
                
            }
                
                break;
                
            case 1:{
                
                //创建登录控制器
                HZCaseManageViewController* caseManageVc = [[HZCaseManageViewController alloc]init];
                
                caseManageVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:caseManageVc animated:NO];
                
            }
                
                break;
                
            case 2:{
                
                //创建登录控制器
                HZFocusDoctorViewController* focusDoctorVc = [[HZFocusDoctorViewController alloc]init];
                
                focusDoctorVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:focusDoctorVc animated:NO];
                
            }
                
                break;
        }
        
        
    }else{
        
        switch (indexPath.row) {
            case 0:{
                
                //创建登录控制器
                HZSettingAndHelpViewController* settingAndHelpVc = [[HZSettingAndHelpViewController alloc]init];
                
                settingAndHelpVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:settingAndHelpVc animated:NO];
                
            }
                
                break;
                
            case 1:{
                
                //创建登录控制器
                HZAboutProductViewController* aboutProductVc = [[HZAboutProductViewController alloc]init];
                
                aboutProductVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:aboutProductVc animated:NO];
                
            }
                
                break;
                
            case 2:{
                
                //创建登录控制器
                HZSharedFriendViewController* sharedFriendVc = [[HZSharedFriendViewController alloc]init];
                
                sharedFriendVc.navigationItem.title = model.title;
                
                //跳转到登录控制器
                [mainVC.navigationController pushViewController:sharedFriendVc animated:NO];
                
            }
                
                break;
        }

        
    }
  
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.array[section] count];
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //取出数据
    HZPreferencesModel* model = self.array[indexPath.section][indexPath.row];
    
 
    //取出 cell
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"preCell" forIndexPath:indexPath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //赋值
    cell.textLabel.text = model.title;
    
    
    return  cell;
}





















#pragma mark -
#pragma mark     接收通知
-(void)receiveNoti{
    
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLogin:) name:@"login" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRegister:) name:@"register" object:nil];
    
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark     实现通知方法
-(void)didLogin:(NSNotification*)noti{
    
    
    HZMainViewController *mainVC = [[HZMainViewController alloc]init];
    
    
    [self.mm_drawerController setCenterViewController:[[UINavigationController alloc]initWithRootViewController:mainVC] withCloseAnimation:YES completion:nil];
    
    //创建登录控制器
    HZLoginVc* loginVc = [[HZLoginVc alloc]init];
    
    //跳转到登录控制器
    [mainVC.navigationController pushViewController:loginVc animated:NO];
    
}

-(void)didRegister:(NSNotification*)noti{
    
    
    
    HZMainViewController *mainVC = [[HZMainViewController alloc]init];
    
    
    [self.mm_drawerController setCenterViewController:[[UINavigationController alloc]initWithRootViewController:mainVC] withCloseAnimation:YES completion:nil];
    
    //创建登录控制器
    HZRegisterVc* registerVc = [[HZRegisterVc alloc]init];
    
    //跳转到登录控制器
    [mainVC.navigationController pushViewController:registerVc animated:NO];

    
}



@end
